package cn.itedus.lottery.domain.rule.service.logic;

import cn.itedus.lottery.common.Constants;
import cn.itedus.lottery.domain.rule.model.req.DecisionMatterReq;
import cn.itedus.lottery.domain.rule.model.vo.TreeNodeLineVO;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName: BaseLogic
 * @Description: 规则基础抽象类
 * @Author: szq
 * @Date: 2021-11-03 13:42:07
 */
public abstract class BaseLogic implements LogicFilter{

    @Override
    public Long filter(String matterValue, List<TreeNodeLineVO> treeNodeLineInfoList) {
        AtomicReference<Long> nodeIdTo = new AtomicReference<>(Constants.Global.TREE_NULL_NODE);
        treeNodeLineInfoList.stream().forEach(t->{
            if (decisionLogic(matterValue, t)){
                 nodeIdTo.set(t.getNodeIdTo());
            }
        });
        return nodeIdTo.get();
    }

    @Override
    public abstract String matterValue(DecisionMatterReq decisionMatter);

    private boolean decisionLogic(String matterValue, TreeNodeLineVO nodeLine){
        String ruleLimitValue = nodeLine.getRuleLimitValue();
        switch (nodeLine.getRuleLimitType()){
            case Constants.RuleLimitType.EQUAL:
                return matterValue.equals(ruleLimitValue);
            case Constants.RuleLimitType.GT:
                return Double.parseDouble(matterValue) > Double.parseDouble(ruleLimitValue);
            case Constants.RuleLimitType.LT:
                return Double.parseDouble(matterValue) < Double.parseDouble(ruleLimitValue);
            case Constants.RuleLimitType.GE:
                return Double.parseDouble(matterValue) >= Double.parseDouble(ruleLimitValue);
            case Constants.RuleLimitType.LE:
                return Double.parseDouble(matterValue) <= Double.parseDouble(ruleLimitValue);
            default:
                return false;
        }
    }
}
